package com.eliota.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eliota.common.R;
import com.eliota.entity.Device;
import com.eliota.entity.Type;
import com.eliota.service.TypeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MG
 * @since 2023-04-01
 */
@RestController
@RequestMapping("/type")
public class TypeController {

    @Resource
    private TypeService typeService;


    @GetMapping("/page")
    public R findPage(@RequestParam Integer pageNum,
                      @RequestParam Integer pageSize,
                      @RequestParam String typeName){
        Page<Type> typePage = new Page<>(pageNum, pageSize);
        QueryWrapper<Type> typeQueryWrapper = new QueryWrapper<>();
        typeQueryWrapper.like("type_name",typeName).orderByAsc("type_id");

        return R.success(typeService.page(typePage,typeQueryWrapper));
    }


    @PostMapping("/save")
    public R save(@RequestBody Type type){
        System.out.println(type.toString());
        return R.success(typeService.saveOrUpdate(type) );
    }


    /*userNam*/
    @GetMapping("/{id}")
    public R findByUserName(@PathVariable Integer typeId){
        Type getTypeByName = typeService.getOne(new QueryWrapper<Type>().eq("type_id", typeId));
        return R.success(getTypeByName);
    }



    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id){
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>"+id);
        typeService.removeById(id);
        return R.success();
    }

    @PostMapping("/bath/del")
    public R deleteBath(@RequestBody List<Integer> ids){
        typeService.removeBatchByIds(ids);
        return R.success();
    }






}
